package com.fiberhome.platform.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.fiberhome.platform.common.PageResult;
import com.fiberhome.platform.entity.DesignNeedOut;
import com.fiberhome.platform.mapper.DesignNeedMapper;
import com.fiberhome.platform.mapper.NeedCategoryMapper;
import com.fiberhome.platform.pojo.DesignNeed;
import com.fiberhome.platform.pojo.DesignNeedExample;
import com.fiberhome.platform.pojo.NeedCategory;
import com.fiberhome.platform.pojo.NeedCategoryExample;
import com.fiberhome.platform.pojo.NeedCategoryExample.Criteria;

@Service
public class NeedServiceImpl implements NeedService {

	@Autowired
	private DesignNeedMapper designNeedMapper;

	@Autowired
	private NeedCategoryMapper needCategoryMapper;

	@Override
	@Transactional
	public PageResult saveNeed(DesignNeed need) {
		// 首先将设计需求编号更新到需求分类中
		NeedCategory needCat = needCategoryMapper.selectByPrimaryKey(need
				.getId());
		needCat.setDesignNo(need.getDesignNo());
		needCategoryMapper.updateByPrimaryKeySelective(needCat);

		// 插入设计需求记录
		// DesignNeed的id首先设为空，因为之前的id仅仅是用于接收前台的id参数的，并不是测试用例的实际id
		need.setId(null);
		need.setCreatedTime(new Date());
		need.setUpdatedTime(new Date());
		NeedCategoryExample example = new NeedCategoryExample();
		Criteria criteria = example.createCriteria();
		criteria.andDesignNoEqualTo(need.getDesignNo());
		List<NeedCategory> needCats = needCategoryMapper
				.selectByExample(example);

		List<NeedCategory> parentList = needCategoryMapper
				.selectAllParent(needCats.get(0).getId());
		int size = parentList.size();

		if (size == 0) {

		} else if (size == 1) {
		} else if (size == 2) {
		} else if (size == 3) {
			need.setNeedClass1(parentList.get(1).getId());
			need.setNeedClass2(parentList.get(2).getId());
		} else if (size == 4) {
			need.setNeedClass1(parentList.get(1).getId());
			need.setNeedClass2(parentList.get(2).getId());
			need.setNeedClass3(parentList.get(3).getId());
		} else if (size == 5) {
			need.setNeedClass1(parentList.get(1).getId());
			need.setNeedClass2(parentList.get(2).getId());
			need.setNeedClass3(parentList.get(3).getId());
			need.setNeedClass4(parentList.get(4).getId());
		} else if (size == 6) {
			need.setNeedClass1(parentList.get(1).getId());
			need.setNeedClass2(parentList.get(2).getId());
			need.setNeedClass3(parentList.get(3).getId());
			need.setNeedClass4(parentList.get(4).getId());
			need.setNeedClass5(parentList.get(5).getId());
		}
		if (designNeedMapper.insert(need) > 0) {
			return PageResult.ok(need);
		} else {
			return PageResult.ok();
		}
	}

	@Override
	@Transactional
	public PageResult editNeed(DesignNeed need) {
		NeedCategoryExample example = new NeedCategoryExample();
		Criteria criteria = example.createCriteria();
		criteria.andDesignNoEqualTo(need.getDesignNo());
		List<NeedCategory> needCat = needCategoryMapper.selectByExample(example);
		
		List<NeedCategory> parentList = needCategoryMapper.selectAllParent(needCat.get(0).getId());
		int size = parentList.size();

		if (size == 0) {

		} else if (size == 1) {
		} else if (size == 2) {
		} else if (size == 3) {
			need.setNeedClass1(parentList.get(1).getId());
			need.setNeedClass2(parentList.get(2).getId());
		} else if (size == 4) {
			need.setNeedClass1(parentList.get(1).getId());
			need.setNeedClass2(parentList.get(2).getId());
			need.setNeedClass3(parentList.get(3).getId());
		} else if (size == 5) {
			need.setNeedClass1(parentList.get(1).getId());
			need.setNeedClass2(parentList.get(2).getId());
			need.setNeedClass3(parentList.get(3).getId());
			need.setNeedClass4(parentList.get(4).getId());
		} else if (size == 6) {
			need.setNeedClass1(parentList.get(1).getId());
			need.setNeedClass2(parentList.get(2).getId());
			need.setNeedClass3(parentList.get(3).getId());
			need.setNeedClass4(parentList.get(4).getId());
			need.setNeedClass5(parentList.get(5).getId());
		}
		//need = (DesignNeed) CopyNotNull.copyTo(pre, need);
		need.setUpdatedTime(new Date());
		if (designNeedMapper.updateByPrimaryKey(need) > 0) {
			return PageResult.ok(need);
		} else {
			return PageResult.ok();
		}
	}

	@Override
	public DesignNeedOut getNeedById(Integer id) {
		DesignNeedOut need = designNeedMapper.selectByPrimaryKey(id);
		return need;
	}

	@Override
	public DesignNeedOut getNeedByNo(String no) {
		DesignNeedExample example = new DesignNeedExample();
		DesignNeedExample.Criteria criteria = example.createCriteria();
		criteria.andDesignNoEqualTo(no);
		List<DesignNeedOut> re = designNeedMapper.selectByExample(example);
		return re.get(0);
	}

	@Override
	@Transactional
	public PageResult deleteNeed(Integer id) {
		try {
			NeedCategory needCat = needCategoryMapper.selectByPrimaryKey(id);
			needCategoryMapper.deleteByPrimaryKey(id);
			String num = needCat.getDesignNo();
			DesignNeedExample example = new DesignNeedExample();
			com.fiberhome.platform.pojo.DesignNeedExample.Criteria criteria = example
					.createCriteria();
			criteria.andDesignNoEqualTo(num);
			designNeedMapper.deleteByExample(example);
			return PageResult.ok(needCat);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return PageResult.build(500, "删除异常");
	}

	@Override
	public PageResult isDup(String designNo) {
		DesignNeedExample example = new DesignNeedExample();
		DesignNeedExample.Criteria criteria = example.createCriteria();
		criteria.andDesignNoEqualTo(designNo);
		List<DesignNeedOut> result = designNeedMapper.selectByExample(example);
		if (result.size() == 0) {
			return PageResult.ok();
		}
		return PageResult.ok(result.get(0));
	}

}
